﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Tetris.Model;
using Tetris.View;

namespace Tetris.Controler
{
    public class TetrisControler: ITetrisControler
    {
        public TetrisControler(ITetrisModel model)
        {
            var view = new TetrisView(this, model);

            this._model = model;
            this._view = view;
            this._view.BindCurrentModelToView(_model.CurrentElement);
            this._view.BindNextModelToView(_model.NextElement);

            this._model.AllBlocksCollection.ElementFromCollectionDeleted += this._view.OnBlockDeleted;
            this._model.AllBlocksCollection.CollectionLineDeleted += this._view.OnLineDeleted;


            this._view.Show();

            var t = new Thread( this._view.StartGame );
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        #region private fields

        private ITetrisModel _model;
        private TetrisView _view;

        #endregion

        #region private methods

        #endregion

        #region ITetrisControler implementation

        public void BindCurrentModelToView(IElementModel elementModel)
        {
            this._view.Dispatcher.Invoke(new Action(() => this._view.BindCurrentModelToView(elementModel)));
        }

        public void BindNextModelToView(IElementModel elementModel)
        {
            this._view.Dispatcher.Invoke(new Action(() => this._view.BindNextModelToView(elementModel)));
        }

        public void UserCommand(string command)
        {
            switch(command)
            {
                case "Right":
                    if (this._model.CanElementMoveRight(this._model.CurrentElement))
                    {
                        this._model.MoveRight();
                    }
                    break;
                case "Left":
                    if(this._model.CanElementMoveLeft(this._model.CurrentElement))
                    {
                        this._model.MoveLeft();
                    }
                    break;
                case "Down":
                    if (this._model.CanElementMoveDown(this._model.CurrentElement))
                    {
                        this._model.MoveDown();
                    }
                    else
                    {
                        this._model.AddLastBlockToCollection();
                        this._model.DeleteFullLines(this._model.CurrentElement);
                        this._model.GetNextElement();

                        this.BindCurrentModelToView(this._model.CurrentElement);
                        this.BindNextModelToView(this._model.NextElement);
                    }
                    break;
                default:
                    if (this._model.CanElementRotate(this._model.CurrentElement))
                    {
                        this._model.Rotate();
                    }
                    break;
            }
        }

        #endregion

    }
}
